const api = require('../../api/api')
const app = getApp()
const util = require('../../utils/js/util')
Page({
  data: {
    safebottom: util.getSafeToBottom(),
    list: [],
    listStatus: 'loading',
    page: 1,
    popshow: false,
    editId: '',
    name: '',
    sexs: [{
      title: '男',
      value: '1'
    }, {
      title: '女',
      value: '2'
    }],
    sex: 0,
    birthday: '2000-01-01'
  },
  onLoad() {
    this.getClientList()
  },
  onReachBottom() {
    if (this.data.listStatus === 'loading') {
      this.setData({ page: this.data.page + 1 })
      this.getClientList()
    }
  },
  // 获取就诊人列表
  async getClientList() {
    const res = await api.getClientList({ p: this.data.page })
    const newdata = res.data.map(v => ({ ...v, age: new Date().getFullYear() - new Date(v.birthday).getFullYear() }))
    this.setData({
      list: this.data.page == 1 ? newdata : this.data.list.concat(newdata),
      listStatus: res.last_page > res.current_page ? 'loading' : res.last_page == res.current_page ? 'tobottom' : res.last_page < res.current_page ? 'nodata' : ''
    })
  },
  // 编辑
  edit({ currentTarget: { dataset: { item } } }) {
    this.setData({ editId: item.id, popshow: true, sex: item.gender, birthday: item.birthday, name: item.name })
  },
  // 删除
  del({ currentTarget: { dataset: { item } } }) {
    wx.showModal({
      title: '是否删除该就诊人',
      confirmText: '删除',
      confirmColor: '#ff542d',
      success: async e => {
        if (e.confirm) {
          await api.delClient({ ids: [`${item.id}`] })
          util.toast('删除成功')
          this.setData({ listStatus: 'loading', page: 1 })
          this.getClientList()
          // 上一个页面刷新就诊人
          const res = getCurrentPages()
          const route = res[res.length - 2]
          console.log(route.route)
          if(route.route === "pages/doctor/index" || route.route === "pages/purchased/index" || route.route === "pages/patientSelect/index") {
            route.setData({ page: 1, listStatus: 'loading' })
            route.getPatient()
          }
        }
      }
    })
  },
  // 生日改变
  birthdayChange({ detail: { value } }) {
    this.setData({ birthday: value })
  },
  // 性别改变
  sexChange({ detail: { value } }) {
    this.setData({ sex: value })
  },
  // 添加就诊人弹窗
  addpatient() {
    this.setData({ popshow: true })
  },
  // 关闭弹窗
  popclose() {
    this.setData({ editId: '', name: '', sex: 0, birthday: '2000-01-01', popshow: false })
  },
  // 确定
  async confirm() {
    if (this.data.editId) {
      const res = await api.updateClient({
        name: this.data.name,
        gender: `${this.data.sex}`,
        birthday: this.data.birthday,
        id: `${this.data.editId}`,
      })
      util.toast('修改成功');
    } else {
      const res = await api.addClient({
        name: this.data.name,
        gender: `${this.data.sex}`,
        birthday: this.data.birthday
      })
      util.toast('新增成功');
    }
    this.popclose();
    this.setData({ listStatus: 'loading', page: 1 })
    this.getClientList()
    // 上一个页面刷新就诊人
    const res = getCurrentPages()
    const route = res[res.length - 2]
    console.log(route.route)
    if(route.route === "pages/doctor/index" || route.route === "pages/purchased/index" || route.route === "pages/patientSelect/index") {
      route.setData({ page: 1, listStatus: 'loading' })
      route.getPatient()
    }
  },
})
